package com.qianxun.module.datasource1.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qianxun.module.datasource1.admin.entity.SysMenuRoleRelation;
import com.qianxun.module.datasource1.admin.mapper.SysMenuRoleRelationMapper;
import com.qianxun.module.datasource1.admin.service.SysMenuRoleRelationService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 角色菜单mapping 服务实现类
 * </p>
 *
 * @author admin
 * @since 2019-11-13
 */
@Service
public class SysMenuRoleRelationServiceImpl extends ServiceImpl<SysMenuRoleRelationMapper, SysMenuRoleRelation> implements SysMenuRoleRelationService {

    @Override
    public List<Long> listMenuIdByRoleId(Long roleId) {
       return  this.baseMapper.listMenuIdByRoleId(roleId);
    }

    @Override
    public void removeByRoleId(Long roleId) {
        Wrapper wrapper = new QueryWrapper<SysMenuRoleRelation>().lambda().eq(SysMenuRoleRelation::getRoleId ,roleId);
        remove(wrapper);
    }

    @Override
    public void removeByMenuId(Long menuId) {
        Wrapper wrapper = new QueryWrapper<SysMenuRoleRelation>().lambda().eq(SysMenuRoleRelation::getMenuId ,menuId);
        remove(wrapper);
    }

    @Override
    public void setAuthority(Long roleId, Long[] menuIds) {
        //删除老的权限
        removeByRoleId(roleId);

        List<SysMenuRoleRelation> list = new ArrayList<>(menuIds.length);
        for(int i = 0 ;i < menuIds.length ;++i){
            if(menuIds[i] == 0){
                continue;
            }
            SysMenuRoleRelation sysRoleMenuRelation = new SysMenuRoleRelation();
            sysRoleMenuRelation.setRoleId(roleId).setMenuId(menuIds[i]);
            list.add(sysRoleMenuRelation);
        }
        saveBatch(list);
    }
}
